﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace BlankAbp.EntityFrameworkCore
{
    /**
     *  EF Core 控制台命令必须要有该类
     *  (类似 Add-Migration 和 Update-Database 命令)
     * */
    public class BlankAbpDbContextFactory : IDesignTimeDbContextFactory<BlankAbpDbContext>
    {
        public BlankAbpDbContext CreateDbContext(string[] args)
        {
            /**
             * 实体扩展属性映射相关的配置
             * 这里没有配置，也用不上
             * 所以注释掉
             * */
            //CRMEfCoreEntityExtensionMappings.Configure();

            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<BlankAbpDbContext>()
                .UseSqlServer(configuration.GetConnectionString("Default"));

            return new BlankAbpDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../BlankAbp.DbMigrator/"))
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
